package com.hulman.oms.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.hulman.oms.bean.VehicleRefuel;
import com.hulman.oms.bean.VehicleRepair;
import com.hulman.oms.bean.VehicleUse;
import com.hulman.oms.util.DateUtil;
import com.hulman.oms.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

@Service
@Slf4j
public class VehicleReportService
{
    @Autowired
    private VehicleUseService vehicleUseService;
    @Autowired
    private VehicleRefuelService vehicleRefuelService;
    @Autowired
    private VehicleRepairService vehicleRepairService;

    /**
     * 根据查询条件导出用车报表
     *
     * @param vehicleName
     * @param driverName
     * @param startDateRange
     * @param outputStream
     */
    public void exportVehicleReports(String vehicleName, String driverName, String startDateRange, OutputStream outputStream)
    {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtil.isEmpty(startDateRange))
        {
            String[] dates = startDateRange.split(" - ");
            map.put("startDate", dates[0]);
            map.put("endDate", dates[1]);
        }
        map.put("vehicleName", vehicleName);
        map.put("driverName", driverName);
        List<VehicleUse> vehicleUses = vehicleUseService.findVehicleUses(map);
        List<VehicleRefuel> vehicleRefuels = vehicleRefuelService.findVehicleRefuels(map);
        List<VehicleRepair> vehicleRepairs = vehicleRepairService.findVehicleRepairs(map);
        //时间正排
        Collections.reverse(vehicleUses);
        Collections.reverse(vehicleRefuels);
        Collections.reverse(vehicleRepairs);
        Set<String> vehicles = new HashSet<>();
        for (int i = 0; i < vehicleUses.size(); i++)
        {
            VehicleUse vehicleUse = vehicleUses.get(i);
            vehicleUse.setSn(i + 1);
            vehicles.add(vehicleUse.getVehicleName());
        }
        for (int i = 0; i < vehicleRefuels.size(); i++)
        {
            VehicleRefuel vehicleRefuel = vehicleRefuels.get(i);
            vehicleRefuel.setSn(i + 1);
            vehicles.add(vehicleRefuel.getVehicleName());
        }
        for (int i = 0; i < vehicleRepairs.size(); i++)
        {
            VehicleRepair vehicleRepair = vehicleRepairs.get(i);
            vehicleRepair.setSn(i + 1);
            vehicles.add(vehicleRepair.getVehicleName());
        }
        Map<String, Object> cover = new HashMap<>();
        cover.put("vehicles", String.join("、", vehicles));
        if (map.containsKey("startDate"))
        {
            cover.put("startDate", DateUtil.toChineseDate(DateUtil.parseDate(map.get("startDate").toString())));
        }
        if (map.containsKey("endDate"))
        {
            cover.put("endDate", DateUtil.toChineseDate(DateUtil.parseDate(map.get("endDate").toString())));
        }
        cover.put("editDate", DateUtil.toChineseDate(DateUtil.now()));
        InputStream inputStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("template/vehicle-report.xlsx");
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();
        WriteSheet sheet0 = EasyExcel.writerSheet().sheetNo(0).build();//封面
        WriteSheet sheet1 = EasyExcel.writerSheet().sheetNo(1).build();//出车
        WriteSheet sheet2 = EasyExcel.writerSheet().sheetNo(2).build();//加油
        WriteSheet sheet3 = EasyExcel.writerSheet().sheetNo(3).build();//维修
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(cover, sheet0);
        excelWriter.fill(vehicleUses, fillConfig, sheet1);
        excelWriter.fill(vehicleRefuels, fillConfig, sheet2);
        excelWriter.fill(vehicleRepairs, fillConfig, sheet3);
        excelWriter.finish();
    }
}
